package com.hxkcloud.nurse.popup;

import android.app.Activity;
import android.graphics.drawable.ColorDrawable;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.PopupWindow;

import com.hxkcloud.nurse.R;
import com.hxkcloud.nurse.utils.BrightnessUtils;
import com.hxkcloud.nurse.wheelview.OnWheelScrollListener;
import com.hxkcloud.nurse.wheelview.WheelView;
import com.hxkcloud.nurse.wheelview.adapter.ArrayWheelAdapter;

/**
 * Created by ray_lei on 2016/7/14.
 * 选择天数
 */
public class DaysSelectPopWindow extends PopupWindow implements View.OnClickListener, OnWheelScrollListener {

    private Activity context;
    private WheelView wheel_days;

    private Button bt_cancel;
    private Button bt_sure;

    private String address[] = new String[]{"1天", "2天", "3天", "4天", "5天", "6天", "7天","8天", "9天", "10天", "11天", "12天", "13天", "14天","15天", "16天", "17天", "18天", "19天", "20天", "21天","22天", "23天", "24天", "25天", "26天", "27天", "28天","29天","30天","31天","32天","33天","34天","35天","36天","37天","38天","39天","40天","41天","42天","43天","44天","45天","46天","47天","48天","49天","50天"};

    public DaysSelectPopWindow(Activity context) {
        this.context = context;
        View rootView = LayoutInflater.from(context).inflate(R.layout.popup_selectdays, null);
        setContentView(rootView);

        setWidth(ViewGroup.LayoutParams.MATCH_PARENT);
        setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);

        setFocusable(true);
        setTouchable(true);
        setOutsideTouchable(true);
        setBackgroundDrawable(new ColorDrawable(0x00000000));

        setAnimationStyle(R.style.popupwindow_anim_default);
        initViews(rootView);
        initDatas();
        initListener();
    }

    private void initViews(View rootView) {
        wheel_days = (WheelView) rootView.findViewById(R.id.wheel_selectdays_add);
        bt_cancel = (Button) rootView.findViewById(R.id.bt_selectdays_cancel);
        bt_sure = (Button) rootView.findViewById(R.id.bt_selectdays_sure);
//        wheel_addr.setDrawShadows(false);
//        wheel_addr.setBackgroundColor(ContextCompat.getColor(context, R.color.white));

    }

    private void initDatas() {

//        NumericWheelAdapter numericWheelAdapter1 = new NumericWheelAdapter(context, 1970, curYear);
//        numericWheelAdapter1.setLabel(context.getString(R.string.pop_timeSelector_year));
//        wheel_year.setViewAdapter(numericWheelAdapter1);
//        wheel_year.setCyclic(true);//是否可循环滑动
//
//        NumericWheelAdapter numericWheelAdapter2 = new NumericWheelAdapter(context, 1, 12, "%02d");
//        numericWheelAdapter2.setLabel(context.getString(R.string.pop_timeSelector_month));
//        wheel_month.setViewAdapter(numericWheelAdapter2);
//        wheel_month.setCyclic(true);

        ArrayWheelAdapter adapter = new ArrayWheelAdapter(context, address);
        wheel_days.setViewAdapter(adapter);
        wheel_days.setCyclic(true);
        wheel_days.setCurrentItem(29);

        //设置显示行数
        wheel_days.setVisibleItems(7);

    }

    private void initListener() {
        bt_sure.setOnClickListener(this);
        bt_cancel.setOnClickListener(this);
        setOnDismissListener(new OnDismissListener() {
            @Override
            public void onDismiss() {
                BrightnessUtils.lightOn(context);
            }
        });
    }

    public void show(View parent) {
        showAtLocation(parent, Gravity.BOTTOM, 0, 0);
        BrightnessUtils.lightOff(context);
    }

    @Override
    public void onClick(View v) {
        dismiss();
        switch (v.getId()) {
            case R.id.bt_selectdays_sure:
                if (daysListener != null)
                    daysListener.setDays(address[wheel_days.getCurrentItem()]);
                break;
            case R.id.bt_selectdays_cancel:

                break;
        }
    }

    @Override
    public void onScrollingStarted(WheelView wheel) {

    }

    @Override
    public void onScrollingFinished(WheelView wheel) {

    }

    public interface DaysListener {
        void setDays(String days);
    }

    private DaysListener daysListener;

    public void setDaysListener(DaysListener daysListener) {
        this.daysListener = daysListener;
    }
}
